{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "40118120",
   "metadata": {},
   "source": [
    "# 列表的基本方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0edd7163",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3387cabe",
   "metadata": {},
   "source": [
    "## 1.增加的方法\n",
    "### 1.1 增加 append( )  增加在list的末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "88c97301",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'xinyi']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.append(\"xinyi\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e7bf529",
   "metadata": {},
   "source": [
    "### 1.2 插入 insert() 可以在指定位置增加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b95ac768",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert之后的names： ['Mike', 'Mary', 'xinyi', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.insert(2,'xinyi')\n",
    "print(\"insert之后的names：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e4bdac4",
   "metadata": {},
   "source": [
    "## 2. 删除 remove  和 pop\n",
    "### 优势：如果删除list中多个元素时，remove比较合适\n",
    "### 2.1 remove() 的操作应该先做查询，判断元素value是否存在？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "80f53b3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： ['Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "if \"Mike\" in names:\n",
    "    names.remove(\"Mike\")\n",
    "print(\"remove删除的结果：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bc4ae0b",
   "metadata": {},
   "source": [
    "### 2.2 pop( ) 删除位置为1的这个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2f498c9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的names: ['Mike', 'Mary', 'Jan']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0257ba83",
   "metadata": {},
   "source": [
    "## 3. index( )索引 查看该元素如 10001 在stu_id中第一个出现的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "968d12b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "print(stu_id.index(10001))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cdc3fb5",
   "metadata": {},
   "source": [
    "## 4. clear( )清除所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "00474793",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eacb16b7",
   "metadata": {},
   "source": [
    "## 5.count( )查看列表元素出现的个数，（简单应用）统计分数出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "571db2ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法的结果 10001 有: 3\n",
      "count方法的结果 Mary 有: 1\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "print(\"count方法的结果 10001 有:\",stu_id.count(10001))\n",
    "print(\"count方法的结果 Mary 有:\",names.count(\"Mary\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15813032",
   "metadata": {},
   "source": [
    "## 6. extend( ) 是给指定的list进行扩充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f7857a9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 10001, 10002, 10003, 10004, 10008, 10001, 10009, 10010, 10001]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "names = [\"Mike\", \"Mary\", \"Jan\", \"Jack\"]\n",
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
